//
//  ATJgAdsBiddingDelegate.m
//  HeadBiddingDemo
//
//  Created by lix on 2022/10/20.
//

#import "ATJgAdsBiddingDelegate.h"
#import "ATJgAdsBiddingManager.h"
#import "ATJgAdsBiddingRequest.h"

#import <JgAdsSDK/JgAdsSDK.h>
#import <JgAdsSDK/JgAdsSplashAd.h>
#import <JgAdsSDK/JgAdsRewardVodAd.h>
#import <AnyThinkSplash/AnyThinkSplash.h>
//开屏容器
#import "JgAdsSplashAdContainer.h"
//插屏容器
#import "JgAdsInterstitialAdContainer.h"
//激励视频容器
#import "JgAdsRewardVodAdContainer.h"
//注意：根据需要删除未使用到的容器和容器相应代理即可
@interface ATJgAdsBiddingDelegate () <JgAdsSplashAdContainerDelegate,JgAdsInterstitialAdContainerDelegate,JgAdsRewardVodAdContainerDelegate>

@end

@implementation ATJgAdsBiddingDelegate
- (void)jgads_loadSuccess:(id)ad withPrice:(NSInteger)bidPrice{
    ATJgAdsBiddingRequest *request = [[ATJgAdsBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {

        NSString *price = @(bidPrice/100.0f).stringValue;
#if DEBUG
        NSLog(@"jgads============竞价价格：%@,广告位：%@",price,request.placementID);
#endif
        //注意：slot_id为后台配置的广告源key
        
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID unitGroupUnitID:request.unitGroup.unitID adapterClassString:request.unitGroup.adapterClassString price:price currencyType:ATBiddingCurrencyTypeCNY expirationInterval:request.unitGroup.networkTimeout customObject:ad];
        request.bidCompletion(bidInfo, nil);
    }
    
    [[ATJgAdsBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}
- (void)jgads_loadFailLoadError:(NSError *)error{
    ATJgAdsBiddingRequest *request = [[ATJgAdsBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        request.bidCompletion(nil, error);
    }
    [[ATJgAdsBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}
#pragma mark -- JgAdsSplashAdContainerDelegate
/**
 *  开屏广告素材加载成功
 */
- (void)jgadsContainer_jgadsSplashAdDidLoad:(JgAdsSplashAdContainer *)splashAd withPrice:(NSInteger)bidPrice {
    NSLog(@"%s", __FUNCTION__);
    [self jgads_loadSuccess:splashAd withPrice:bidPrice];
    
}

/**
 *  开屏广告请求失败
 */
- (void)jgadsContainer_jgadsSplashAdFailLoadError:(NSError *)error{
    NSLog(@"%s %@", __FUNCTION__ ,error);
    [self jgads_loadFailLoadError:error];
}
#pragma mark -- JgAdsInterstitialAdContainerDelegate
/**
 *  插屏广告素材加载成功
 */
- (void)jgadsContainer_jgadsInterstitialAdDidLoad:(nonnull JgAdsInterstitialAdContainer *)InterstitialAd withPrice:(NSInteger)bidPrice {
    [self jgads_loadSuccess:InterstitialAd withPrice:bidPrice];
}

/**
 *  插屏广告请求失败
 */
- (void)jgadsContainer_jgadsInterstitialAdFailLoadError:(nonnull NSError *)error {
    [self jgads_loadFailLoadError:error];
}

#pragma mark -- JgAdsRewardVodAdContainerDelegate
- (void)jgadsContainer_jgadsRewardVodAdDidLoad:(nonnull JgAdsRewardVodAdContainer *)RewardVodAd withPrice:(NSInteger)bidPrice {
    [self jgads_loadSuccess:RewardVodAd withPrice:bidPrice];
}

- (void)jgadsContainer_jgadsRewardVodAdFailLoadError:(nonnull NSError *)error {
    [self jgads_loadFailLoadError:error];
}

@end
